import Vue from 'vue'
import Router from 'vue-router'

// import Login from '../components/LoginU'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/LoginU')
// import Home from '../components/HomeU'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/HomeU')
// import Welcome from '../components/WelcomeU'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/WelcomeU')


// import Users from '../components/user/UsersU'
const Users = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/user/UsersU')
// import Rights from '../components/power/RightsU'
const Rights = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/RightsU')
// import Roles from '../components/power/RolesU'
const Roles = () => import(/* webpackChunkName: "users_rights_roles" */ '../components/power/RolesU')

// import Cate from '../components/goods/CateU'
const Cate = () => import(/* webpackChunkName: "cate_params" */ '../components/goods/CateU')
// import Params from '../components/goods/ParamsU'
const Params = () => import(/* webpackChunkName: "cate_params" */ '../components/goods/ParamsU')

// import GoodsList from '../components/goods/GoodsU'
const GoodsList = () => import(/* webpackChunkName: "goodsList_addGoods" */ '../components/goods/GoodsU')
// import AddGoods from '../components/goods/AddGoods'
const AddGoods = () => import(/* webpackChunkName: "goodsList_addGoods" */ '../components/goods/AddGoods')

// import Order from '../components/order/OrderU'
const Order = () => import(/* webpackChunkName: "order_report" */ '../components/order/OrderU')
// import Report from '../components/report/ReportU'
const Report = () => import(/* webpackChunkName: "order_report" */ '../components/report/ReportU')


Vue.use(Router)


const router =  new Router({
  routes:[
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {
      path: '/home', 
      component: Home,
      redirect:'/welcome',
      children:[
        {path:'/welcome',component:Welcome},
        {path:'/users',component:Users},
        {path:'/rights',component:Rights},
        {path:'/roles',component:Roles},
        {path:'/categories',component:Cate},
        {path:'/params',component:Params},
        {path:'/goods',component:GoodsList},
        {path:'/goods/add',component:AddGoods},
        {path:'/orders',component:Order},
        {path:'/reports',component:Report}
    ]},
  ]
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // to 将要访问的路径
  // from 从那个路径跳转
  // next 是一个函数 表示放行
  //    next() 放行 next('./login') 强制跳转
  if(to.path === '/login') return next()
  // 获取token
  const tokenStr = sessionStorage.getItem('token')
  if(!tokenStr) return next('./login')
  next()

})




export default router
